package com.itstyle.quartz.service.impl;

import com.itstyle.quartz.entity.Auth;
import com.itstyle.quartz.entity.Menu;
import com.itstyle.quartz.entity.MenuTreeBuilder;
import com.itstyle.quartz.mapper.AuthMapper;
import com.itstyle.quartz.mapper.MenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MenuService {

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private AuthMapper authMapper;

    public List<Menu> findByUserId(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        String username = userDetails.getUsername();
        Auth byusername = authMapper.byusername(username);
        List<Menu> menus = menuMapper.findByUserId(byusername.getAut());
        return MenuTreeBuilder.buildTree(menus);
    }
}
